package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.server.manger.cross.CrossDataMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigInteger;
import java.util.Collection;

@Cmd(code = GameProtocol.S_CROSS_GENERAL_ACTIVITY_UNION_RANK_LIST, desc = "")
public class CrossGeneralActivityUnionRankCmd extends AbstractCommand {
    private int activityId;
    private int conditionType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdActivityUserRankListReqMsg req = StrongholdWarProto.StrongholdActivityUserRankListReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        conditionType = req.getConditionType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 加一个请求频率限制
        if (player.isRequestCmdTooFast(packet.getCode(),300)){
            return;
        }
       /* if (conditionType == StrongholdActivityMgr.CONDITION_TYPE_SILVER_UNION) {
            //如果是银两捐献 且不是捐献时间，直接读本地吧
            if (StrongholdActivityMgr.isAfterWildTime()) {
                sendLocalData(player);
                return;
            }
        }*/

       /* StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.Builder msg = StrongholdWarProto.CrossStrongholdActivityUnionRankListReqMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setConditionType(conditionType);
        UnionActivityConditionData conditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, conditionType, player.getUserId());
        String unionId = "";
        if (conditionData != null) {
            unionId = conditionData.getUnionUid();
        }
        msg.setUnionUid(unionId);
        player.sendChannelPacket(Protocol.C_CROSS_GENERAL_UNION_RANK, msg);*/

        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            // 商会分组活动走这边
            if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
                builder.setActivityId(activityId);
                builder.setGroupId(GameUnionActivityGroupMgr.getViewDataGroupId(activityId, player.getUserId()));
                builder.setIdKey(String.valueOf(conditionType));
                player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_LIST, builder, activityId);
                return;
            }
        }

        CrossUnionActivityRankDataProto.CrossUnionActivityDataMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionActivityDataMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setUserId(player.getUserId());
        msg.setConditionType(conditionType);
        msg.setRankListType(2);
        player.sendPacket(CrossProtocol.C_CROSS_UNION_ACTIVITY_RANK_LIST, msg);

    }

//    private void sendLocalData(GamePlayer player) {
//
//        String unionUid = player.getUserInfo().getUnionUid();
//        UnionActivityRankProto.UnionActivityRankListMsg tempList = CrossDataMgr.getCrossActivityUnionConditionTypeRankList(activityId, conditionType);
//        StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.Builder msg = StrongholdWarProto.StrongholdActivityUnionRankListRespMsg.newBuilder();
//        msg.setMyRank(StrongholdWarMgr.getSilverRank(activityId, unionUid));
//        Collection<String> qualifyUnionList = StrongholdWarMgr.getQualifyUnionList(activityId, true);
//        BigInteger myUnionValue = BigInteger.ZERO;
//        UnionActivityRankProto.UnionActivityRankListMsg.Builder rankListMsg = UnionActivityRankProto.UnionActivityRankListMsg.newBuilder();
//        for (UnionActivityRankProto.UnionActivityRankTempMsg tempMsg : tempList.getRankListList()) {
//            if (qualifyUnionList.contains(tempMsg.getUnionUid())) {
//                rankListMsg.addRankList(tempMsg);
//            }
//            if (tempMsg.getUnionUid().equals(unionUid)) {
//                myUnionValue = new BigInteger(tempMsg.getValue());
//            }
//        }
//        msg.setValue(myUnionValue.toString());
//        msg.setRankList(rankListMsg);
//        player.sendChannelPacket(ClientProtocol.U_STRONGHOLD_UNION_RANK, msg);
//    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
